package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserQuery;
import com.tarena.lbs.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class UserRepositoryImpl implements UserRepository {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserPO getByPhone(String phone) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Long countUserByPhone(String phone) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("phone",phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public void save(UserPO poParam) {
        userMapper.insert(poParam);
    }

    @Override
    public UserPO getById(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public PageInfo<UserPO> pagesList(UserQuery query) {
        QueryWrapper<UserPO> queryWrapper = assembleUserQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<UserPO> pos = userMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        log.debug("用户列表业务层方法，出参:{}", pos);
        return new PageInfo<>(pos);
    }

    @Override
    public UserVO selectByUserId(Integer userId) {
        log.debug("用户详情业务层方法，入参:{}", userId);
        UserPO userPO = userMapper.selectById(userId);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userPO,userVO);
        log.debug("用户详情业务层方法，出参:{}", userVO);
        return userVO;
    }

    @Override
    public UserVO updateUser(UserParam param) {
        log.debug("用户详情业务层方法，入参:{}", param);
       UpdateWrapper<UserPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",param.getId());
        if (StringUtils.isNotBlank(param.getPhone())){
            updateWrapper.set("phone",param.getPhone());
        }
        if (StringUtils.isNotBlank(param.getUserName())){
            updateWrapper.set("user_name",param.getUserName());
        }
        if (StringUtils.isNotBlank(param.getNickName())){
            updateWrapper.set("nick_name",param.getNickName());
        }
        if (StringUtils.isNotBlank(param.getProviderType())){
            updateWrapper.set("provider_type",param.getProviderType());
        }
        if (StringUtils.isNotBlank(param.getUserImage())){
            updateWrapper.set("user_image",param.getUserImage());
        }
        if (StringUtils.isNotBlank(param.getPassword())){
            updateWrapper.set("password",param.getPassword());
        }
        if (StringUtils.isNotBlank(param.getIntroduction())){
            updateWrapper.set("introduction",param.getIntroduction());
        }
        if (StringUtils.isNotBlank(param.getIpAddress())){
            updateWrapper.set("ip_address",param.getIpAddress());
        }
        userMapper.update(null,updateWrapper);
        return selectByUserId(param.getId());
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.deleteById(userId);
    }

    private QueryWrapper<UserPO> assembleUserQuery(UserQuery query) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        //1.accountPhone 账号手机号 相等查询 where account_phone=#{phone}
        if (StringUtils.isNotBlank(query.getAccountPhone())){
            queryWrapper.like("account_phone",query.getAccountPhone());
        }
        //2.nickname 昵称 模糊查询 非空 where nickname like(%#{}%)
        if (StringUtils.isNotBlank(query.getUserName())){
            queryWrapper.like("user_name",query.getUserName());
        }
        if (StringUtils.isNotBlank(query.getProviderType())){
            queryWrapper.like("provider_type",query.getProviderType());
        }
        if (query.getRegStartTime()!=null){
            queryWrapper.ge("reg_time",query.getRegStartTime());
        }
        if (query.getRegEndTime()!=null){
            queryWrapper.le("reg_time",query.getRegEndTime());
        }
        return queryWrapper;
    }

}
